package com.asialjim.wechat.official.message.handler.message;

import com.asialjim.wechat.apiservice.constant.WeChatCacheName;
import com.asialjim.wechat.database.DataBaseService;
import com.asialjim.wechat.message.WeChatMessage;
import com.asialjim.wechat.message.handler.DefaultWeChatMessageHandler;
import com.asialjim.wechat.message.output.WeChatOutputMessage;
import com.asialjim.wechat.msgcrypt.WxBizMsgCryptService;
import com.asialjim.wechat.official.message.menu.MsgMenuInfo;
import com.asialjim.wechat.official.message.wiki.MsgWikiFunction;
import com.asialjim.wechat.official.message.wiki.dto.MsgWikiInfo;
import com.asialjim.wechat.official.message.wiki.MsgWikiType;
import com.asialjim.wechat.official.message.wiki.dto.MsgWikiUrl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;

import java.util.Map;
import java.util.Objects;

/**
 * <h1><em>ASIAL JIM JAVA DOC</em></h1><hr/>
 * <h2>CLASS DESCRIPTION <i>[ NAME: WikiMessageHandler ]</i></h2><strong>
 * 知识库处理
 * <p> wiki message handler
 * </strong><p><p><em>Copyright &copy; Asial Jim Co., LTD</em><hr/>
 *
 * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
 * @version 1.0.0
 * @since 2021/3/24   &nbsp;&nbsp; JDK 8
 */
@Slf4j
public abstract class WikiMessageHandler extends DefaultWeChatMessageHandler {
    private final DataBaseService dataBaseService;
    private final Map<String, MsgWikiFunction> wikiFunctionMap;
    private final RedissonClient redissonClient;

    public WikiMessageHandler(WxBizMsgCryptService cryptService,DataBaseService dataBaseService,Map<String,MsgWikiFunction> wikiFunctionMap,RedissonClient redissonClient) {
        super(cryptService);
        this.dataBaseService = dataBaseService;
        this.wikiFunctionMap = wikiFunctionMap;
        this.redissonClient = redissonClient;
    }

    protected WeChatOutputMessage handleWikiMessage(WeChatMessage wechatMessage,String content){

        MsgWikiInfo msgWikiInfo = dataBaseService.selectOne(MsgWikiInfo.builder().wiki(content).build());
        if (Objects.isNull(msgWikiInfo)){
            msgWikiInfo = dataBaseService.selectOne(MsgWikiInfo.builder().id("0").build());
            log.info("\r\n\tdefault result:[{}]",msgWikiInfo);
            return result(wechatMessage, msgWikiInfo, null);
        }

        MsgWikiUrl msgWikiUrl = dataBaseService.selectOne(MsgWikiUrl.builder().id(msgWikiInfo.getId()).build());
        if (MsgWikiType.function.equals(msgWikiInfo.getType())){
            if (Objects.isNull(wikiFunctionMap)){
                log.warn("\r\n\tWIKI FUNCTION REGISTRY UNABLE USE!!!");
                return result(wechatMessage,msgWikiInfo,msgWikiUrl);
            }
            MsgWikiFunction msgWikiFunction = wikiFunctionMap.get(msgWikiInfo.getContent());
            if (Objects.isNull(msgWikiFunction)){
                log.warn("\r\n\tWIKI FUNCTION FOR KEY-WORD:[{}] IS NOT ONLINE",content);
                return result(wechatMessage,msgWikiInfo,msgWikiUrl);
            }
            return msgWikiFunction.execute(wechatMessage);
        }

        return result(wechatMessage, msgWikiInfo, msgWikiUrl);
    }

    protected WeChatOutputMessage handleWikiMessage(WeChatMessage callMessage){
        RMapCache<String, MsgMenuInfo> mapCache = redissonClient.getMapCache(WeChatCacheName.WECHAT_MESSAGE_MENU);
        MsgMenuInfo msgMenuInfo = mapCache.get(callMessage.getFromUserName());
        String wikiFunBeanName = msgMenuInfo.getWikiFun();
        MsgWikiFunction msgWikiFunction = wikiFunctionMap.get(wikiFunBeanName);
        return msgWikiFunction.execute(callMessage);
    }


    private WeChatOutputMessage result(WeChatMessage wechatMessage, MsgWikiInfo msgWikiInfo, MsgWikiUrl msgWikiUrl) {
        try {
            return MsgWikiType.nameOf(msgWikiInfo.getType()).outputMessage(msgWikiInfo, msgWikiUrl);
        } catch (Exception e) {
            return null;
        }
    }
}
